package cn.kgc.kgde.bookstore.controller;

import cn.kgc.kgde.bookstore.dto.CartDTO;
import cn.kgc.kgde.bookstore.entity.*;
import cn.kgc.kgde.bookstore.mapper.CartMapper;
import cn.kgc.kgde.bookstore.mapper.OrderMapper;
import cn.kgc.kgde.bookstore.service.BookService;
import cn.kgc.kgde.bookstore.service.CartService;
import cn.kgc.kgde.bookstore.service.CartitemService;
import cn.kgc.kgde.bookstore.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author myt
 * @since 2022-05-30
 */
@CrossOrigin
@Configuration
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    /**
     *
     * @param order
     * @param pageNum
     * @return
     * 查看订单 -- 后台 -- 查看所有 -- 条件 -- 根据订单状态查询
     */
    @PostMapping("/showOrder")
    public ResultInfo showOrder(@RequestParam(defaultValue = "1") Integer pageNum, Order order){
        IPage<Order> orderList = orderService.findOrder( order,pageNum,15);
        if (orderList.getRecords()==null){
            return new ResultInfo(500,"查询用户出现错误··！");
        }
        return new ResultInfo( 200,"订单信息查询成功！",orderList);
    }

    //修改订单状态 - 后台 操作订单状态信息
    @PostMapping("/modify/{orderId}/{state}")
    public ResultInfo modifyOrder(@PathVariable String orderId,@PathVariable Integer state){
        orderService.modify( orderId,state );
        return new ResultInfo( 200,"订单状态修改完成！");
    }

    //生成订单 - 后台 -购物车结算生成订单
    @Resource
    private BookService bookService;
    @Resource
    private CartitemService cartitemService;
    @PostMapping("/addOrder")
    public ResultInfo addOrder(Order order,Long  cartId,String userCode){
        System.out.println("order = " +order);
        List<CartDTO> carts =this.orderService.cartDTOList(order,userCode);
        Order order1= this.orderService.add(order,carts);
        Book book = new Book();
        //根据购物详情循环修改商品
        for (CartDTO record : carts) {
            book.setId( record.getBookId() );
            book.setStock( record.getQuantity() );
            System.out.println("book = " + book);
            //修改商品库存
            this.bookService.modify(book);
        }
        Cartitem cartitem = new Cartitem();
        cartitem.setCartId(cartId);
        this.cartitemService.deleteById(cartitem);

        return new ResultInfo(200,"购物车商品结算，生成订单",order1);
    }
    @Resource
    private OrderMapper orderMapper;
    //查看订单 - 前台 -根据UserCode查看
    @GetMapping("showOrder2")
    public ResultInfo showOrder0(String userCode){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userCode",userCode);
        List<Order> orderList = this.orderMapper.selectList(queryWrapper);
        return new ResultInfo(200,"查询成功",orderList);
    }
}

